<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
        <el-col :span="10">
          <el-form-item label="所属站点" prop="deptId" style="width: 100%;">
            <el-tree-select
                v-model="queryParams.deptId"
                :data="deptOptions"
                :props="{ value: 'deptId', label: 'deptName', children: 'children' }"
                value-key="deptId"
                placeholder="请选择所属站点"
                check-strictly
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['partybuilding:NewsType:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" @handleQuery="handleQuery" @resetQuery="resetQuery"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="NewsTypeList" @selection-change="handleSelectionChange">
      <el-table-column label="栏目名称" align="left" prop="name" width="370"/>
      <el-table-column label="导航栏显示" align="left" prop="remark" width="300"/>
      <el-table-column label="排序" align="left" prop="sort" width="250"/>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template #default="scope">
          <!--<el-button link type="danger" @click="handleAdd(scope.row)" v-hasPermi="['partybuilding:NewsType:add']">添加下级栏目</el-button>
          <el-divider direction="vertical" />-->
          <el-button link type="danger" @click="handleUpdate(scope.row)" v-hasPermi="['partybuilding:NewsType:edit']">修改</el-button>
          <el-divider direction="vertical" />
          <el-button link type="danger" @click="handleDelete(scope.row)" v-hasPermi="['partybuilding:NewsType:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改新闻栏目对话框 -->
    <el-dialog :title="title" v-model="open" width="750px" append-to-body>
      <el-form ref="NewsTypeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="所属站点:" prop="deptId" style="width: 100%;">
              <el-tree-select
                v-model="form.deptId"
                :data="deptOptions"
                :props="{ value: 'deptId', label: 'deptName', children: 'children' }"
                value-key="deptId"
                placeholder="请选择所属站点"
                check-strictly
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="父级栏目" prop="parentName">
              <el-input v-model="form.parentName" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序" prop="sort">
              <el-input-number v-model="form.sort" placeholder="请输入排序" style="width: 50%;" :min="0" :max="99" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="栏目名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入栏目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" :rows="8"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="NewsType">
import { listNewsType, getNewsType, delNewsType, addNewsType, updateNewsType } from "@/api/partybuilding/NewsType";
import { getUserProfile } from "@/api/system/user";
import { listOrg } from "@/api/partybuilding/Org";
const { proxy } = getCurrentInstance();

const NewsTypeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptOptions = ref([]);
const userInfo = ref({}); //用户信息

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: null,
    orderByName: "sort",
    descIs: false
  },
  rules: {
    name: [
      { required: true, message: "栏目名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

// 获取用户信息
function getUserInfo() {
  getUserProfile().then((res) => {
    userInfo.value = res.data;
  });
}

/** 查询部门下拉树结构 */
function getDeptTree() {
  listOrg().then(response => {
    deptOptions.value = proxy.handleTree(response.rows, "deptId");
    if (deptOptions.value!=null && deptOptions.value.length>0){
      queryParams.value.deptId = deptOptions.value[0].deptId;
    }
  });
};

/** 查询新闻栏目列表 */
function getList() {
  loading.value = true;
  listNewsType(queryParams.value).then(response => {
    NewsTypeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    parentId: null,
    remark: null,
    sort: null,
    deptId: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("NewsTypeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  if(row!=null){
    form.value.parentId = row.id;
    form.value.parentName = row.name;
  }
  form.value.deptId = queryParams.value.deptId;
  open.value = true;
  title.value = "添加栏目";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getNewsType(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改栏目";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["NewsTypeRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateNewsType(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNewsType(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除栏目编号为"' + _ids + '"的数据项？').then(function() {
    return delNewsType(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('partybuilding/NewsType/export', {
    ...queryParams.value
  }, `NewsType_${new Date().getTime()}.xlsx`)
}

getUserInfo();
getDeptTree();
setTimeout(() => {
  getList();
}, 100);
</script>
